package com.example.demo.member.controller;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.member.model.Dog;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private Environment environment;

    @GetMapping("/test")
    public String test() {
        String port = environment.getProperty("local.server.port");
        String string = "[member], port: " + port + ", time: " + LocalDateTime.now();
        System.out.println(string);
        return string;
    }

    @GetMapping("/{type}/{id}")
    public List<Dog> get(@PathVariable String type, @PathVariable Integer id) {

        String port = environment.getProperty("local.server.port");
        String string = "[member], port: " + port + ", type: " + type + ", id: " + id;
        System.out.println(string);

        Dog dog = new Dog();
        dog.setId(id);
        dog.setType(type);
        dog.setSize(BigDecimal.valueOf(3.1415923));

        Dog dog1 = new Dog();
        dog1.setId(2223);
        dog1.setType("poke face");
        dog1.setSize(BigDecimal.valueOf(54321.123456));

        Dog dog2 = new Dog();
        dog2.setId(3);
        dog2.setType("fffffaaa");
        dog2.setSize(BigDecimal.valueOf(3.1423));

        ArrayList<Dog> dogs = new ArrayList<>();
        dogs.add(dog);
        dogs.add(dog1);
        dogs.add(dog2);

        return dogs;
    }

    @GetMapping("/getById")
    public String getById(Integer id) {
        String port = environment.getProperty("local.server.port");
        String string = "[member], port: " + port + ", id: " + id;
        System.out.println(string);
        return string;
    }

    @PostMapping("/save")
    public void save(@RequestBody Dog dog) {
        System.out.println("dog: " + dog);
    }

    @PostMapping("/saveBatch")
    public void save(@RequestBody List<Dog> list) {
        System.out.println("list: " + list);
    }

}
